var currentTask = null;

$(function(){
	$("#choose_homework_select").bind("change", onClickChooseHomeworkSelect);
	$("#sort_homework_button").bind("click", function(){
		showSortHomeworksDiv(currentTask);
	});
	$("#save_sort_button").bind("click", saveSortHomeworkResult);
});

function onClickChooseHomeworkSelect() {
	$("#sort_homework_div").hide();
	var task_id = $(this).val();
	$.post("student/showHomeworksByGroupAndTask", {
		"task.id": task_id
	},function(result){
		var data = JSON.parse(result);
		showMyGroupHomeworks(data);
		currentTask = data;
	});
}

function showMyGroupHomeworks(task) {
	$("#group_homeworks_table").empty();
	addHeaderToGroupHomeworksTable();
	for(var i in task.homeworks) {
		addOneLineToGroupHomeworksTable(task.homeworks[i]);
	}
}

function addOneLineToGroupHomeworksTable(homework){
	var tr = $("<tr/>").attr("homework_id", homework.id);
	var member_td = $("<td/>").text(homework.student.name).appendTo(tr);
	var status_td = $("<td/>").text("Submited").appendTo(tr);
	var url_td = $("<td/>").addClass("rank").text(homework.rank).appendTo(tr);
	var score_td = $("<td/>").text(homework.score).appendTo(tr);
	var option = $("<td/>").text("").appendTo(tr);
	var view_link = $("<a/>").text("view").attr("target", "_blank").attr("href", "deploy/" + COURSE_NAME + "/" + homework.task.id + "/" + homework.student.id + "/index.html");
	var download_link = $("<a/>").attr("href", "teacher/download?filename=" + homework.filename).html('<img src="public/images/download.jpg" alt="download" />');
	option.append(view_link);
	option.append(download_link);
	$("#group_homeworks_table").append(tr);
}

function addHeaderToGroupHomeworksTable() {
	var tr = $("<tr/>");
	var labels = ["Member", "Status", "Rank", "Final Score", "Option"];
	for(var i in labels) {
		var th = $("<th/>").html(labels[i]);
		tr.append(th);
	}
	$("#group_homeworks_table").append(tr);
}


function showSortHomeworksDiv(task) {
	$("#sort_homework_div").show();
	$("#sortable").empty();
	for(var i in task.homeworks) {
		addLiToSortableList(task.homeworks[i]);
	}
}

function addLiToSortableList(homework) {
	var li = $("<li/>").addClass("ui-state-default").attr("homework_id", homework.id);
	li.html('<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + homework.student.name);
	$("#sortable").append(li);
}

function saveSortHomeworkResult() {
	var result = [];
	$("#sortable li").each(function(){
		var homework_id = $(this).attr("homework_id");
		result.push(homework_id);
	});
	$.post("student/sortHomeworks", {
		"task.id" : currentTask.id,
		"sortResult" : result.join()
	}, function(data){
		doAfterSaveSortHomeworkResult(result);
	});
}

function doAfterSaveSortHomeworkResult(result) {
	for(var i = 0; i < result.length; i++) {
		$("#group_homeworks_table tr").each(function(){
			var homework_id = $(this).attr("homework_id");
			if(homework_id == result[i]) {
				$(this).find(".rank").text(i + 1);
			}
		});
	}
	$("#sort_homework_div").hide();
}
